package com.hz.foodstalls.model;

import java.util.ArrayList;
import java.util.List;

import com.hz.foodstalls.config.Constant;
import com.hz.foodstalls.model.base.BaseWechatRobotMedia;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.weixin.sdk.msg.in.InMsg;
import com.jfinal.weixin.sdk.msg.in.event.InFollowEvent;
import com.jfinal.weixin.sdk.msg.out.News;
import com.jfinal.weixin.sdk.msg.out.OutImageMsg;
import com.jfinal.weixin.sdk.msg.out.OutMsg;
import com.jfinal.weixin.sdk.msg.out.OutNewsMsg;
import com.jfinal.weixin.sdk.msg.out.OutTextMsg;
import com.jfinal.weixin.sdk.msg.out.OutVideoMsg;
import com.jfinal.weixin.sdk.msg.out.OutVoiceMsg;

/**
 * Model, Generated by Robot on 2017-8-25 15:39:45.
 */
@SuppressWarnings("serial")
public class WechatRobotMedia extends BaseWechatRobotMedia<WechatRobotMedia> {
    public static final WechatRobotMedia dao = new WechatRobotMedia();

    public Page<WechatRobotMedia> page(int pageNumber, int pageSize) {
        return WechatRobotMedia.dao.paginate(pageNumber, pageSize, "select *",
                "from wechat_robot_media order by id asc");
    }

    public Page<WechatRobotMedia> page(int pageNumber, int pageSize, String id) {
        return WechatRobotMedia.dao.paginate(pageNumber, pageSize, "select *",
                "from wechat_robot_media where wechat_robot_id = ?", id);
    }

    public Page<WechatRobotMedia> page(int pageNumber, int pageSize, String orderField, String orderDirection,
            String where) {
        return WechatRobotMedia.dao.paginate(pageNumber, pageSize, "select * ", String
                .format("from wechat_robot_media where 1=1 %s order by %s %s", where, orderField, orderDirection));
    }

    public int batchDelete(String ids) {
        return Db.update(String.format("delete from wechat_robot_media where id in (%s) ", ids));
    }

    public boolean saveOrUpdate(WechatRobotMedia model, boolean isSave) {
        boolean flag = false;
        if (model.getId() == null) {
            // model.setCreateTime(new Date());
            flag = model.save();
        } else {
            // model.setEditTime(new Date());
            flag = model.update();
        }
        return flag;
    }

    public List sendnews(List<WechatRobotMedia> wrms) {
        List newslist = new ArrayList();
        for (int i = 0; i < wrms.size(); i++) {
            News news = new News();
            WechatRobotMedia wrm = wrms.get(i);
            news.setTitle(wrm.getTitle());
            news.setDescription(wrm.getDescription());
            news.setPicUrl(wrm.getPicUrl());
            news.setUrl(wrm.getUrl());
            newslist.add(news);
        }
        return newslist;
    }

    public List getAllMedias(int wrid) {
        return null;
    }

    // 判断回复类型
    public OutMsg buildOutMessage(List<WechatRobotMedia> medias, InMsg inTextMsg) {
        if (medias.size() > 1) {
            // 图文
            OutNewsMsg outmsg = new OutNewsMsg(inTextMsg);
            List<News> list = sendnews(medias);
            outmsg.setArticles(list);
            return outmsg;
        } else {
            return buildOutMessage(medias.get(0), inTextMsg);
        }
    }

    public OutMsg buildOutMessage(WechatRobotMedia m, InMsg inTextMsg) {
        if (null == m) {
            if (inTextMsg instanceof InFollowEvent)
                return new OutTextMsg(inTextMsg).setContent(Constant.WECHAT_ROBOT_DEFAULT_SUBSCRIBE);
            return new OutTextMsg(inTextMsg).setContent(Constant.WECHAT_ROBOT_DEFAULT_TEXT);
        }
        if (m.getReplyContentType() == Constant.WECHAT_ROBOT_TEXT) {
            OutTextMsg msg = new OutTextMsg(inTextMsg);
            msg.setContent(m.getContentText());
            return msg;
        }
        if (m.getReplyContentType() == Constant.WECHAT_ROBOT_IMAGE) {
            OutImageMsg msg = new OutImageMsg(inTextMsg);
            msg.setMediaId(m.getMediaId());
            return msg;
        }
        if (m.getReplyContentType() == Constant.WECHAT_ROBOT_VOICE) {
            OutVoiceMsg msg = new OutVoiceMsg(inTextMsg);
            msg.setMediaId(m.getMediaId());
            return msg;
        }
        if (m.getReplyContentType() == Constant.WECHAT_ROBOT_VIDEO) {
            OutVideoMsg msg = new OutVideoMsg(inTextMsg);
            msg.setMediaId(m.getMediaId());
            return msg;
        }
        if (m.getReplyContentType() == Constant.WECHAT_ROBOT_NEWS) {
            OutNewsMsg msg = new OutNewsMsg(inTextMsg);
            List<WechatRobotMedia> newsmsg = new ArrayList<WechatRobotMedia>();
            newsmsg.add(m);
            List<News> list = sendnews(newsmsg);
            msg.setArticles(list);
            return msg;
        }
        return null;
    }
}
